/*************************************************************************
	> File Name: v4l2.c
	> Author: 
	> Mail: 
	> Created Time: Tue 03 Jul 2018 02:31:34 PM CST
 ************************************************************************/

#include "convert.h"





void initcamera(int *cammerFd)
{
    //打开视频设备
    //*cammerFd = open("/dev/video0",O_RDWR|O_NONBLOCK,0);
	*cammerFd = open("/dev/video0",O_RDWR,0);
    if(*cammerFd<0)
    {
        perror("open error");
        return ;
    }
    //查看设备属性
    struct v4l2_capability cap;
    int ret = ioctl(*cammerFd, VIDIOC_QUERYCAP, &cap);
    if (ret < 0) 
    {
        printf("VIDIOC_QUERYCAP failed (%d)\n", ret);
        return ;
            
    }
    printf("Capability Informations:\n");
    printf(" driver: %s\n", cap.driver);//
    printf(" card: %s\n", cap.card);
    printf(" bus_info: %s\n", cap.bus_info);
    printf(" version: %08X\n", cap.version);
    printf(" capabilities: %08X\n", cap.capabilities);

    //设置视频格式
    struct v4l2_format fmt;
    memset(&fmt, 0, sizeof(fmt));
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//
    fmt.fmt.pix.width = VIDEO_WIDTH;
    fmt.fmt.pix.height = VIDEO_HEIGHT;
    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
    fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
    ret = ioctl(*cammerFd, VIDIOC_S_FMT, &fmt);
    if (ret < 0)
    {
        printf("VIDIOC_S_FMT failed (%d)\n", ret);
        return ;
            
    }
    // 获取视频格式
    ret = ioctl(*cammerFd, VIDIOC_G_FMT, &fmt);
    if (ret < 0) 
    {
        printf("VIDIOC_G_FMT failed (%d)\n", ret);
        return ;
    }
    // Print Stream Format
    printf("Stream Format Informations:\n");
    printf(" type: %d\n", fmt.type);
    printf(" width: %d\n", fmt.fmt.pix.width);
    printf(" height: %d\n", fmt.fmt.pix.height);
    char fmtstr[8];
    memset(fmtstr, 0, 8);
    memcpy(fmtstr, &fmt.fmt.pix.pixelformat, 4);
    printf(" pixelformat: %s\n", fmtstr);
    printf(" field: %d\n", fmt.fmt.pix.field);
    printf(" bytesperline: %d\n", fmt.fmt.pix.bytesperline);
    printf(" sizeimage: %d\n", fmt.fmt.pix.sizeimage);
    printf(" colorspace: %d\n", fmt.fmt.pix.colorspace);
    printf(" priv: %d\n", fmt.fmt.pix.priv);
    printf(" raw_date: %s\n", fmt.fmt.raw_data);
	return ;
}


    //分配内存
    
int fenpei(struct v4l2_requestbuffers *reqbuf, int *cammerFd)
{
    reqbuf->count = BUFFER_COUNT;
    reqbuf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    reqbuf->memory = V4L2_MEMORY_MMAP;
    int ret = ioctl(*cammerFd, VIDIOC_REQBUFS, reqbuf);
    if(ret < 0)
    {
        printf("VIDIOC_REQBUFS failed (%d)\n", ret);
        return ret;
        
    }
	return 0;
}
	
	
    //获取并记录缓存的内存空间 
	//framebuf = calloc(reqbuf.count, sizeof(*framebuf));

  /*  for (int i = 0; i < reqbuf.count; i++) 
    {
        buf.index = i;
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
        ret = ioctl(cammerFd, VIDIOC_QUERYBUF, &buf);
        if(ret < 0) 
        {
            printf("VIDIOC_QUERYBUF (%d) failed (%d)\n", i, ret);
            return ret;
        }
    
        //mmap buffer
        framebuf[i].length = buf.length;
        framebuf[i].start = (char *) mmap(0, buf.length, PROT_READ|PROT_WRITE, MAP_SHARED, cammerFd, buf.m.offset);
        if (framebuf[i].start == MAP_FAILED)
        {
            printf("mmap (%d) failed: %s\n", i, strerror(errno));
            return -1;
     
        }
        //queue buffer
        ret = ioctl(cammerFd, VIDIOC_QBUF, &buf);
        if (ret < 0) 
        {
            printf("VIDIOC_QBUF (%d) failed (%d)\n", i, ret);
            return -1;       
        }

    printf("Frame buffer %d: address=0x%x, length=%d\n", i, (unsigned int)framebuf[i].start, framebuf[i].length);
    }
*/
	
	/*
    //开始采集
    enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    ret = ioctl(cammerFd, VIDIOC_STREAMON, &type);
    if (ret < 0)
    {
        printf("VIDIOC_STREAMON failed (%d)\n", ret);
        return ret;
        
    }
    // 获得图像
    ret = ioctl(cammerFd, VIDIOC_DQBUF, &buf);
    if (ret < 0) 
    {
        printf("VIDIOC_DQBUF failed (%d)\n", ret);
        return ret;
    }*/
	
	
	
    //显示图像 
    /*FILE *fp = fopen(CAPTURE_FILE, "wb");
    if (fp < 0) 
    {
        printf("open frame data file failed\n");
        return -1;
        
    }
    fwrite(framebuf[buf.index].start, 1, buf.length, fp);
    fclose(fp);
    printf("Capture one frame saved in %s\n", CAPTURE_FILE);
    //re_queen buffer
    ret = ioctl(fd, VIDIOC_QBUF, &buf);
    if (ret < 0) 
    {
        printf("VIDIOC_QBUF failed (%d)\n", ret);
        return ret;
        
    }*/
    
	
	
	

